package com.touchstone.cloudpacs.common.utils.request;

import com.touchstone.cloudpacs.common.utils.CommonUtils;
import com.touchstone.cloudpacs.common.utils.dto.OrderByDTO;

import java.util.List;

/**
 * @Description : 分页查询参数封装
 * @Author : hunter
 * @Date: 2018-11-12 14:37
 * @Version: 1.0
 **/
public class PageRequest<T> {

    /**
     * 页码
     */
    private Integer page;

    /**
     * 页面大小
     */
    private Integer limit;

    /**
     * 默认页码为1
     */
    private static final int DEFAULT_PAGE_NO = 1;

    /**
     * 默认页面大小10
     */
    private static final int DEFAULT_PAGE_SIZE = 10;

    /**
     * 具体业务查询条件封装实体
     */
    private T param;

    /**
     * 排序字段
     */
    private List<OrderByDTO> orderBys;

    public Integer getPage() {
        return CommonUtils.isEmpty(page) ? DEFAULT_PAGE_NO : page;
    }

    public void setPage(Integer page) {
        this.page = page;
    }

    public Integer getLimit() {
        return CommonUtils.isEmpty(limit) ? DEFAULT_PAGE_SIZE : limit;
    }

    public void setLimit(Integer limit) {
        this.limit = limit;
    }

    public T getParam() {
        return param;
    }

    public PageRequest<T> setParam(T param) {
        this.param = param;
        return this;
    }

    public String getOrderBys() {
        if (CommonUtils.isNotEmpty(this.orderBys)) {
            StringBuilder sb = new StringBuilder();
            for (OrderByDTO orderByDTO : orderBys) {
                if (CommonUtils.isNotEmpty(orderByDTO.getOrderField())) {
                    sb.append(orderByDTO.getOrderField());
                    if (!orderByDTO.getAsc()) {
                        sb.append(" desc");
                    }
                    sb.append(" ,");
                }
            }
            String result = sb.toString();
            return CommonUtils.isNotEmpty(result) ? result.substring(0, result.length() - 1) : result;
        }
        return "";
    }

    public void setOrderBys(List<OrderByDTO> orderBys) {
        this.orderBys = orderBys;
    }

}
